package com.unitd.frame.sso.common.token;

import com.alibaba.fastjson.annotation.JSONField;
import com.unitd.frame.sso.common.config.SSOConfig;
import com.unitd.frame.sso.common.helper.IpHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * @desc SSO 登录标记Cookie基本信息对象
 * @filename SSOToken.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/14
 */
@SuppressWarnings("serial")
public class SSOToken extends Token {

	private Integer type; 									// 登录类型
	private String data; 									// 预留
	@JSONField(serialize = false)
	private Object object; 									// 预留对象, 注解表示默认 fastjson 不参与序列化(也就是不存放在 cookie 中)此处配合分布式缓存使用,可以存放用户常用基本信息

	/**
	 * @desc 构造方法
	 */
	public SSOToken() {
		this.setApp(SSOConfig.getInstance().getAppName());
	}

	public SSOToken(HttpServletRequest request) {
		this.setIp(IpHelper.getIpAddr(request));
		this.setApp(SSOConfig.getInstance().getAppName());
	}

	public SSOToken(HttpServletRequest request, String uid) {
		this(request);
		this.setUid(uid);
	}

	public SSOToken(HttpServletRequest request, String uid, Integer type) {
		this(request, uid);
		this.setType(type);
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	/**
	 * @desc 缓存用户信息,自动类型转换
	 * @param <T> 需要进行缓存的对象
	 * @return T
	 */
	@SuppressWarnings("unchecked")
	public <T> T getCacheObject() {
		return (T) this.getObject();
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}
}